package com.haiertvbic.tvnow.network.paser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.haiertvbic.tvnow.model.ProgramDetail;

public class ProductsPaser extends XmlPaser {
	private ArrayList<ProgramDetail> programs = null;

	public ProductsPaser(InputStream result) {
		this.result = result;
	}

	@Override
	public ArrayList<ProgramDetail> getContent() {

		try {
			parser.setInput(result, "UTF-8");

			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
					programs = new ArrayList<ProgramDetail>();
					break;
				case XmlPullParser.START_TAG:// 开始元素事件
					String tagname = parser.getName();
					if ("programInfo".equalsIgnoreCase(tagname)) {
						ProgramDetail program = new ProgramDetail();
						int count = parser.getAttributeCount();
						for (int i = 0; i < count; i++) {
							String attr = parser.getAttributeName(i);
							if ("programId".equalsIgnoreCase(attr)) {
								program.setPid(parser.getAttributeValue(i));
							} else if ("programName".equalsIgnoreCase(attr)) {
								program.setPn(parser.getAttributeValue(i));
							} else if ("programType".equalsIgnoreCase(attr)) {
								program.setPtype(parser.getAttributeValue(i));
							} else if ("js".equalsIgnoreCase(attr)) {
								program.setJs(parser.getAttributeValue(i));
							}
						}
						programs.add(program);
					}
					break;
				case XmlPullParser.END_TAG:// 结束元素事件
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (result != null) {
				try {
					result.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
		return programs;
	}

}
